﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using vngoodlife.Util;

namespace vngoodlife
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Test_New_Singleton",
                "Test_New_Singleton/{id}",
                new { controller = "Article", action = "Test_New_Singleton", id = "" }
            );

            routes.MapRoute(
                "ErrorPage",
                "Error/{errorType}",
                new { controller = "Home", action = "GoToErrorPage", errorType = "" }
            );

            routes.MapRoute(
                "ReadMoreArticleHomePage",
                "Xem-Tiep",
                new { controller = "Home", action = "ArticleListHomePage" }
            );

            routes.MapRoute(
                "TagsFrontend",
                "Tag/{TagsSeoUrl}/{currentPage}",
                new { controller = "Article", action = "GetArticleByTags_Frontend", tagsSeoUrl = "", currentPage = "1" },
                new { tagsSeoUrl = @"[a-zA-Z0-9-]{1,}", currentPage = @"[0-9]{1,}" }
            );

            routes.MapRoute(
                "SubjectFrontend",
                "ky/{SubjectSeoUrl}/{currentPage}",
                new { controller = "Article", action = "GetArticleBySubject_Frontend", subjectSeoUrl = "", currentPage = "1" },
                new { subjectSeoUrl = @"[a-zA-Z0-9-]{1,}", currentPage = @"[0-9]{1,}" }
            );

            routes.MapRoute(
                "ArticleDetailFrontend",
                "Cuoc-Song-Tuoi-Dep/{ArticleSeoUrl}/{currentPage}",
                new { controller = "Article", action = "ArticleDetails", articleSeoUrl = "", currentPage = "1" },
                new { articleSeoUrl = @"[a-zA-Z0-9-]{1,}", currentPage = @"[0-9]{1,}" }
            );

            routes.MapRoute(
                "HeaderMenuFrontend",
                "{menuSeoUrl}/{currentPage}",
                new { controller = "Article", action = "ArticleListFrontend", menuSeoUrl = "", currentPage = "1" },
                new { menuSeoUrl = @"[a-zA-Z0-9-]{1,}", currentPage = @"[0-9]{1,}" }
            );


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            if (Context.User != null)
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                //HttpCookie authCookie = Context.Request.Cookies[cookieName];
                //if (authCookie == null)
                //{
                //    return;
                //}
                //  Response.Cookies.Add(authCookie);
                //       HttpContext.Current.Response.Cookies.Add(authCookie);
                //      authCookie.Expires = DateTime.Now.AddMinutes(ConstantClass.authenCookieTimeLife);

                if (Context.Request.Cookies[cookieName] == null)
                {
                    return;
                }
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(Context.Request.Cookies[cookieName].Value);
                string[] roles = authTicket.UserData.Split(new char[] { '|' });

                //We create a new generic principal based on the roles we defined in the cookie.
                //See SetAuthenticationCookie in AuthenticationService
                FormsIdentity fi = (FormsIdentity)(Context.User.Identity);
                Context.User = new System.Security.Principal.GenericPrincipal(fi, roles);
            }
        }
    }
}